package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.vo.StoreroomAndUserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Storeroom;
import com.ruoyi.system.service.IStoreroomService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库房列表Controller
 * 
 * @author mqs
 * @date 2024-05-01
 */
@Controller
@RequestMapping("/system/storeroom")
public class StoreroomController extends BaseController
{
    private String prefix = "system/storeroom";

    @Autowired
    private IStoreroomService storeroomService;

    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("system:storeroom:view")
    @GetMapping()
    public String storeroom()
    {
        return prefix + "/storeroom";
    }

    /**
     * 查询库房列表列表
     */
    @RequiresPermissions("storeroom:storeroom:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Storeroom storeroom)
    {
        startPage();
        List<StoreroomAndUserVO> list = storeroomService.selectStoreroomList(storeroom);
        return getDataTable(list);
    }

    /**
     * 导出库房列表列表
     */
    @RequiresPermissions("storeroom:storeroom:export")
    @Log(title = "库房列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Storeroom storeroom)
    {
        List<StoreroomAndUserVO> list = storeroomService.selectStoreroomList(storeroom);
        ExcelUtil<StoreroomAndUserVO> util = new ExcelUtil<StoreroomAndUserVO>(StoreroomAndUserVO.class);
        return util.exportExcel(list, "库房列表数据");
    }

    /**
     * 新增库房列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库房列表
     */
    @RequiresPermissions("storeroom:storeroom:add")
    @Log(title = "库房列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Storeroom storeroom,String founderName)
    {
        SysUser sysUser = userService.selectUserByUserName(founderName);
        storeroom.setFounder(sysUser.getUserId());
        return toAjax(storeroomService.insertStoreroom(storeroom));
    }

    /**
     * 修改库房列表
     */
    @RequiresPermissions("storeroom:storeroom:edit")
    @GetMapping("/edit/{storeroomId}")
    public String edit(@PathVariable("storeroomId") Long storeroomId, ModelMap mmap)
    {
        StoreroomAndUserVO storeroom = storeroomService.selectStoreroomByStoreroomId(storeroomId);
        mmap.put("storeroom", storeroom);
        return prefix + "/edit";
    }

    /**
     * 修改保存库房列表
     */
    @RequiresPermissions("storeroom:storeroom:edit")
    @Log(title = "库房列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Storeroom storeroom,String userName)
    {
        SysUser sysUser = userService.selectUserByUserName(userName);
        storeroom.setFounder(sysUser.getUserId());
        return toAjax(storeroomService.updateStoreroom(storeroom));
    }


    /**
     * 查看库房列表
     */
    @RequiresPermissions("storeroom:storeroom:view")
    @GetMapping("/view/{storeroomId}")
    public String view(@PathVariable("storeroomId") Long storeroomId, ModelMap mmap)
    {
        StoreroomAndUserVO storeroom = storeroomService.selectStoreroomByStoreroomId(storeroomId);
        mmap.put("storeroom", storeroom);
        return prefix + "/view";
    }

    /**
     * 删除库房列表
     */
    @RequiresPermissions("storeroom:storeroom:remove")
    @Log(title = "库房列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(storeroomService.deleteStoreroomByStoreroomIds(ids));
    }
}
